package com.uzai.console.web.uzaichat;

import com.alipay.api.domain.DeviceInfo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.sysrole.SysRoleQuery;
import com.uzai.console.dto.tb.tbtoken.TbTokenQuery;
import com.uzai.console.dto.uzaichat.kefuuser.*;
import com.uzai.console.entity.KefuUser;
import com.uzai.console.entity.SysRole;
import com.uzai.console.entity.TbToken;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.KefuUserMapper;
import com.uzai.console.mapper.SysRoleMapper;
import com.uzai.console.mapper.TbTokenMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.uzaichat.KefuUserService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.uzaichat.kefuuser.KefuUserInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月25日 20:15
 */
@Api(value = "客服账号",tags = "客服管理|客服账号")
@ApiSort(20)
@RestController
public class KefuUserContoller extends BaseController {
    private Logger logger = LoggerFactory.getLogger(KefuUserContoller.class);

    @Autowired
    private KefuUserService kefuUserService;
    @Autowired
    private TbTokenMapper tbTokenMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private KefuUserMapper kefuUserMapper;


    @ApiOperation(value = "分页查询客服账号列表", notes = "分页查询客服账号列表", response = KefuUserInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/kefuUser/findKefuUserList")
    public Object findKefuUserList(@RequestBody KefuUserQuery kefuUserQuery){
        return kefuUserService.findKefuUserList(kefuUserQuery);
    }

    @ApiOperation(value = "查询客服账号详情", notes = "查询客服账号详情", response = KefuUserInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/kefuUser/findKefuUserInfo")
    public Object findKefuUserInfo(@RequestBody KefuUserIdDto kefuUserIdDto){
        return kefuUserService.findKefuUserInfo(kefuUserIdDto);
    }

    @SystemLogType(model = SystemModelEnum.KefuUser, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加客服账号", notes = "添加客服账号")
    @ApiOperationSupport(order = 3)
    @PostMapping("/kefuUser/addKefuUser")
    public Object addKefuUser(@RequestBody KefuUserInfoDto kefuUserInfoDto){
        kefuUserService.addKefuUser(kefuUserInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.KefuUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改客服账号", notes = "修改客服账号")
    @ApiOperationSupport(order = 4)
    @PostMapping("/kefuUser/updateKefuUser")
    public Object updateKefuUser(@RequestBody KefuUserInfoDto kefuUserInfoDto){
        kefuUserService.updateKefuUser(kefuUserInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.KefuUser, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除客服账号", notes = "删除客服账号")
    @ApiOperationSupport(order = 5)
    @PostMapping("/kefuUser/deleteKefuUser")
    public Object deleteKefuUser(@RequestBody KefuUserIdDto kefuUserIdDto){
        kefuUserService.deleteKefuUser(kefuUserIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.KefuUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "账号挂起", notes = "账号挂起")
    @ApiOperationSupport(order = 6)
    @PostMapping("/kefuUser/suspendKefuUser")
    public Object suspendKefuUser(@RequestBody KefuUserSuspendDto kefuUserSuspendDto){
        kefuUserService.suspendKefuUser(kefuUserSuspendDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.KefuUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "账号取消挂起", notes = "账号取消挂起")
    @ApiOperationSupport(order = 6)
    @PostMapping("/kefuUser/cancelSuspendKefuUser")
    public Object cancelSuspendKefuUser(@RequestBody KefuUserCancelSuspendDto kefuUserCancelSuspendDto){
        kefuUserService.cancelSuspendKefuUser(kefuUserCancelSuspendDto);
        return true;
    }


    @ApiOperation(value = "查询角色下拉列表", notes = "查询角色下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 7)
    @PostMapping("/kefuUser/findSysSoleListSelect")
    public Object findSysSoleListSelect(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        SysRoleQuery sysRoleQuery = new SysRoleQuery();
        sysRoleQuery.setMerId(getLoginInfo().getMerId());
        List<SysRole> sysRoleList = sysRoleMapper.findByList(sysRoleQuery);
        if(sysRoleList != null && sysRoleList.size() > 0){
            for(SysRole sysRole : sysRoleList){
                SelectKeyVo selectKeyVo =  new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(sysRole.getId()));
                //页面显示
                selectKeyVo.setValue(sysRole.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @ApiOperation(value = "查询个人账号机器人下拉列表", notes = "查询机器人下拉列表", response = DeviceInfoVo.class)
    @ApiOperationSupport(order = 8)
    @PostMapping("/kefuUser/findDeviceInfoListSelect")
    public Object findDeviceInfoListSelect(){
        DeviceQuery deviceQuery = new DeviceQuery();
        deviceQuery.setMerId(getLoginInfo().getMerId());
        List<DeviceInfoVo> deviceInfoList = deviceService.findDeviceListForUnBindKefuUser(deviceQuery);
        return deviceInfoList;
    }

    @ApiOperation(value = "根据设备类型查询机器人下拉列表", notes = "根据设备类型查询机器人下拉列表", response = DeviceInfoVo.class)
    @ApiOperationSupport(order = 8)
    @PostMapping("/kefuUser/findDeviceInfoListSelectByDeviceType")
    public Object findDeviceInfoListSelect(@RequestBody DeviceQuery deviceQuery){
        if(StringUtils.isBlank(deviceQuery.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入设备类型");
        }
        List<DeviceInfoVo> deviceInfoList = deviceService.findDeviceListForUnBindKefuUser(deviceQuery);
        return deviceInfoList;
    }

    @ApiOperation(value = "查询在线账号下拉列表", notes = "查询在线账号下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 9)
    @PostMapping("/kefuUser/findSuspendKefuUserListSelect")
    public Object findSuspendKefuUserListSelect(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        KefuUserQuery kefuUserQuery = new KefuUserQuery();
        kefuUserQuery.setMerId(getLoginInfo().getMerId());
        kefuUserQuery.setStatus(1);//查询在线账号
        List<KefuUser> kefuUserList = kefuUserMapper.findByList(kefuUserQuery);
        if(kefuUserList != null && kefuUserList.size() > 0){
            for (KefuUser kefuUser : kefuUserList){
                SelectKeyVo selectKeyVo =  new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(kefuUser.getId()));
                //页面显示
                selectKeyVo.setValue(kefuUser.getUser());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }



}
